﻿using System;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using Wala.Infrastructure;

namespace Wala.Connection.Objects.Timeline
{
    public class StatusInfo:TimelineInfo
    {
        #region //--------- Private Fields --------------//
        private long _status_id;

        private string _text;

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region //--------- Public Constructors ---------//
        /// <summary>
        /// Initializes a new instance of the <see cref="StatusInfo" /> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="ownerId">The owner id.</param>
        /// <param name="locationText">The location text.</param>
        /// <param name="timeStamp">The time stamp.</param>
        /// <param name="locationX">The location X.</param>
        /// <param name="locationY">The location Y.</param>
        /// <param name="totalComments">The total comments.</param>
        /// <param name="totalLikes">The total likes.</param>
        /// <param name="totalShares">The total shares.</param>
        /// <param name="ownerDisplayName">Display name of the owner.</param>
        /// <param name="ownerAvatarId">The owner avatar id.</param>
        /// <param name="ownerCoverId">The owner cover id.</param>
        /// <param name="isUserLike">The is user like.</param>
        /// <param name="isUserTag">The is user tag.</param>
        /// <param name="isUserFollow">The is user follow.</param>
        /// <param name="text">The text.</param>
        public StatusInfo(long id, int ownerId, string locationText, long timeStamp, double locationX,
            double locationY, int totalComments, int totalLikes, int totalShares,
            string ownerDisplayName, long ownerAvatarId, long ownerCoverId, bool isUserLike,
            bool isUserTag, bool isUserFollow, string text)
            : base(ownerId, ownerAvatarId, ownerCoverId, ownerDisplayName, locationText, locationX, locationY,
                   totalComments, totalLikes, totalShares, timeStamp, isUserLike, isUserTag, isUserFollow)
        {
            _status_id = id;
            _text = text;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StatusInfo" /> class.
        /// </summary>
        public StatusInfo()
        {
        }

        #endregion

        #region //--------- CLR Properties --------------//

        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <value>The type.</value>
        public override int Type
        {
            get
            {
                return URLConstant.TargetType.STATUS;
            }
        }

        public long Status_Id
        {
            get { return _status_id; }
            set { _status_id = value; }
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    OnPropertyChanged("Text");
                }
            }
        }
        #endregion

        #region //--------- Static Methods --------------//
        /// <summary>
        /// Create new StatusInfo object from JSON string.
        /// </summary>
        /// <param name="json">The json string.</param>
        /// <returns>New StatusInfo object.</returns>
        public static StatusInfo FromJson(string json)
        {
            try
            {
                JObject jObject = JObject.Parse(json);

                StatusInfo result = new StatusInfo()
                {
                    Status_Id = (long)jObject["status_id"],

                    Owner_Id = (int)jObject["owner_id"],

                    Text = (string)jObject["text"],
                    Time_Stamp = (long)jObject["time_stamp"],

                    Location_Text = (string)jObject["location_text"],
                    Location_X = (double)jObject["location_x"],
                    Location_Y = (double)jObject["location_y"],

                    Number_Of_Comments = (int)jObject["number_of_comments"],
                    Number_Of_Likes = (int)jObject["number_of_likes"],
                    Number_Of_Shares = (int)jObject["number_of_shares"],

                    Owner_Display_Name = (string)jObject["owner_display_name"],
                    Owner_Avatar_Id = (long)jObject["owner_avatar_id"],
                    Owner_Cover_Id = (long)jObject["owner_cover_id"],

                    User_Like = (bool)jObject["user_like"],
                    User_Tag = (bool)jObject["user_tag"],
                    User_Follow = (bool)jObject["user_follow"]
                };

                return result;
            }
            catch (Exception)
            {
                return new StatusInfo();
            }
        }
        #endregion

        #region //--------- Abstract Class Members ------//

        public override long Id
        {
            get
            {
                return this.Status_Id;
            }
            set
            {
                this.Status_Id = value;
            }
        }

        #endregion

        #region //----------- Private Methods ---------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
